package com.ant.backstage.config.shiro;

import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author 自定义 LogoutFilter shiro 退出
 * @date 2021/12/25 22:28
 **/

public class ShiroLogoutFilter  extends LogoutFilter {

    /**
     * 自定义登出,登出之后,清理当前用户redis部分缓存信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {

        // 登出操作 清除缓存  subject.logout() 可以自动清理缓存信息
        Subject subject = getSubject(request,response);
        PrincipalCollection principals = subject.getPrincipals();
        // 登出
        subject.logout();
        // 获取登出后重定向到的地址
        String redirectUrl = getRedirectUrl(request,response,subject);
        // 重定向
        issueRedirect(request,response,redirectUrl);
        return false;
    }


}
